package com.globefrog.framework.web;


import com.globefrog.framework.log.ActionLogImpl;
import com.globefrog.framework.log.LogHelper;
import com.globefrog.framework.log.handler.ErrorHandler;
import com.globefrog.framework.web.interceptor.RequestBodyInterceptor;
import com.globefrog.framework.web.interceptor.RequestContextInterceptor;
import com.globefrog.framework.web.interceptor.TimeTrackerInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author : caijinbang
 * @date : 2021-03-06
 */
public abstract class AbstractWebConfig implements WebMvcConfigurer {

  @Autowired
  ConfigurableBeanFactory beanFactory;

  public AbstractWebConfig() {
  }


  @Bean
  public LocalValidatorFactoryBean validator() {
    LocalValidatorFactoryBean validatorFactoryBean = new LocalValidatorFactoryBean();
    validatorFactoryBean.getValidationPropertyMap().put("hibernate.validator.fail_fast", "true");
    return validatorFactoryBean;
  }



  @Override
  public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(trackInterceptor());
    registry.addInterceptor(requestContextInterceptor());
    registry.addInterceptor(requestBodyInterceptor());
  }


  @Bean
  public TimeTrackerInterceptor trackInterceptor() {
    return new TimeTrackerInterceptor();
  }


  @Bean
  @Scope(
      value = "request",
      proxyMode = ScopedProxyMode.TARGET_CLASS
  )
  RequestContextServiceImpl requestContext() {
    return new RequestContextServiceImpl();
  }

  @Bean
  public RequestContextInterceptor requestContextInterceptor() {
    return new RequestContextInterceptor();
  }

  @Bean
  public RequestBodyInterceptor requestBodyInterceptor() {
    return new RequestBodyInterceptor();
  }

  @Bean
  ActionLogImpl actionLogger() {
    return ActionLogImpl.getInstance();
  }


  @Bean
  public ErrorHandler errorHandler() {
    return new ErrorHandler();
  }

  @Bean
  public LogHelper logHelper() {
    return new LogHelper();
  }
}